//
//  EightTabbarViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/2.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class EightTabbarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.onCreateUIForView();
    }
    
    //TODO:UI
    fileprivate func onCreateUIForView() {
        self.view.backgroundColor = .white;
        
        //MARK:1.使用系统方法设置
//        let firstVC = FirstViewController();
//        self.onChangeTabbarFromVC(_title: "首页", _img: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), _selectImg: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), _vc: firstVC);
//
//        let centerVC = UIViewController();
//        self.onChangeTabbarFromVC(_title: "二维码", _img: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), _selectImg: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), _vc: centerVC);
//
//        let fiveVC = FiveViewController();
//        self.onChangeTabbarFromVC(_title: "我的", _img: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), _selectImg: UIImage(named: "watch_scanner_fragment_icon") ?? UIImage(), _vc: fiveVC);
        
        //MARK:2.自定义view
        self.tabBar.isHidden = true;
        let tabbarView = EightView(frame: CGRect(x: 0, y: screenMainSize.height-64, width: screenMainSize.width, height: 64));
        self.view.addSubview(tabbarView);
        
        //MARK:3.自定义tabbar
//        self.setValue("", forKeyPath: "tabbar");
    }
    
    //TODO:Actions
    fileprivate func onChangeTabbarFromVC(_title:String,_img:UIImage,_selectImg:UIImage,_vc:UIViewController) {
        if _title == "二维码" {
            _vc.tabBarItem.image = _img.withBaselineOffset(fromBottom: 20);
            _vc.tabBarItem.selectedImage = _selectImg.withBaselineOffset(fromBottom: 20);
            _vc.title = "";
            
        } else {
            let newImg = _img.stretchableImage(withLeftCapWidth: -10, topCapHeight: -10);
            let newSelectImg = _selectImg.stretchableImage(withLeftCapWidth: -10, topCapHeight: -10);
            _vc.tabBarItem.image = newImg.withBaselineOffset(fromBottom: 10);//tabbar图片在基线上偏移量
            _vc.tabBarItem.selectedImage = newSelectImg.withBaselineOffset(fromBottom: 10);
            _vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 10);//tabbar文字左右上下偏移量
            _vc.tabBarItem.title = _title;
            _vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16),NSAttributedString.Key.foregroundColor:UIColor.green], for: .normal);
            _vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16),NSAttributedString.Key.foregroundColor:UIColor.red], for: .selected);
            _vc.tabBarItem.badgeColor = .lightGray;
            _vc.tabBarItem.badgeValue = "12";
        }
        
        self.addChild(_vc);
    }
}
